package com.example.back.service.impl;

import com.example.back.dto.resp.GetCheckTypeList;
import com.example.back.mapper.CheckupTypeMapper;
import com.example.back.service.CheckupTypeService;
import com.example.common.entity.CheckupType;
import com.example.common.support.ApiException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ClassName CheckupTypeServiceImpl
 * @Author wengjt
 * @Date 2021/5/8 10:40
 * @Description
 **/
@Service
public class CheckupTypeServiceImpl implements CheckupTypeService {
    @Resource
    private CheckupTypeMapper checkupTypeMapper;

    /**
     * 多条件查询检查类型
     *
     * @Author wengjt
     * @Date 10:41 2021/5/8
     */
    @Override
    public GetCheckTypeList getCheckTypeList(Integer ctId, Integer showAble, Integer pageNum, Integer pageSize) {
        GetCheckTypeList result = new GetCheckTypeList();
        PageInfo<CheckupType> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            checkupTypeMapper.multipleSearch(ctId, showAble);
        });
        result.setCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 根据id获取单个类型详细
     *
     * @Author wengjt
     * @Date 10:41 2021/5/8
     */
    @Override
    public CheckupType getInfo(Long ctId) {

        return checkupTypeMapper.findOneById(ctId);
    }

    /**
     * 修改检查类型
     *
     * @Author wengjt
     * @Date 10:41 2021/5/8
     */
    @Override
    public Object updateCheckup(CheckupType checkupType) {
        CheckupType oneById = checkupTypeMapper.findOneById(checkupType.getCtId());
        oneById.setName(checkupType.getName());
        oneById.setDisable(checkupType.getDisable());
        oneById.setShowAble(checkupType.getShowAble());
        oneById.setRemark(checkupType.getRemark());
        checkupTypeMapper.saveOne(oneById);
        return null;
    }

    /**
     * 新增检查类型
     *
     * @Author wengjt
     * @Date 10:42 2021/5/8
     */
    @Override
    public Object insertCheckup(CheckupType checkupType) {
        checkupType.setDisable(0);
        checkupType.setCreateTime(new Date());
        checkupTypeMapper.insertOne(checkupType);
        return null;
    }

    /**
     * 删除检查类型
     *
     * @Author wengjt
     * @Date 10:42 2021/5/8
     */
    @Override
    public Object deleteCheckup(Long ctId) {
        CheckupType oneById = checkupTypeMapper.findOneById(ctId);
        if (oneById.getShowAble() == 1) {
            throw new ApiException(400, "展示中的检查类型不允许删除");
        }
        oneById.setDisable(1);
        checkupTypeMapper.saveOne(oneById);
        return null;
    }
}
